package com.example.chenhaosblog.utils;


import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;


public class KingTtoUtil {
    /**
     * @param
     *            :请求接口，凯信通专用
     * @param httpArg
     *            :参数
     * @return 返回结果
     */
    public static String request(String httpUrl, String httpArg) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?" + httpArg;

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            // 填入apikey到HTTP header
            //connection.setRequestProperty("apikey",  "dcf3038e519d9b55cb41eab2efc9be7d");//"dcf3038e519d9b55cb41eab2efc9be7d"--BasicConfig.Instance().getKingttoApiKey()
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    public static boolean SendMsg(String msg,String mobile) throws  Exception {
        try{
            Gson gson = new Gson();
            String httpUrl = "http://sms.kingtto.com:9999/sms.aspx";//http://apis.baidu.com/kingtto_media/106sms/106sms";
            //String verify = String.valueOf((int) (Math.random() * 10)) + String.valueOf((int) (Math.random() * 10)) + String.valueOf((int) (Math.random() * 10)) + String.valueOf((int) (Math.random() * 10));
            String content = msg;//"【中方健康】，您的验证码为" + smsCode + "，10分钟内有效。";
            String content2 = java.net.URLEncoder.encode(content, "utf-8");
            //String httpArg = "mobile=" + mobile + "&content=" + content2 + "&tag=2";
            String httpArg = "mobile=" + mobile + "&content=" + content2 + "&action=send&account=zhongfangzyy@126.com&password=Zftcm999&userid=6331";
            String result = request(httpUrl, httpArg);

        }
        catch (Exception ex){
            throw new Exception(ex.toString());
        }
        return true;
    }

}
